package com.example.child_protection.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.example.child_protection.dao.DepartmentDao;
import com.example.child_protection.dto.info.DepartmentDTO;
import com.example.child_protection.dto.request.SimpleIdRequestDTO;
import com.example.child_protection.dto.request.find.DepartmentRequestDTO;
import com.example.child_protection.dto.request.find.SystemMenuRequestDTO;
import com.example.child_protection.dto.response.DepartmentListResponseDTO;
import com.example.child_protection.dto.response.DepartmentResponseDTO;
import com.example.child_protection.dto.response.SystemMenuListResponseDTO;
import com.example.child_protection.enums.ServiceActionEnums;
import com.example.child_protection.model.DepartmentModel;
import com.example.child_protection.service.DepartmentQueryService;
import com.example.child_protection.service.base.MyBaseService;
import com.example.child_protection.service.base.MyServiceExecutor;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 儿童档案查询service实现层
 *
 * @author Biz
 * @date 2018-11-2
 */
@Component
public class DepartmentQueryServiceImpl extends MyBaseService implements DepartmentQueryService {

    @Autowired
    public DepartmentDao departmentDao;

    /**
     * id查询
     *
     * @param requestDTO
     * @return
     */
    public DepartmentResponseDTO findId(SimpleIdRequestDTO requestDTO) {
        return this.service(ServiceActionEnums.DEPARTMENT_FIND_ID, requestDTO, DepartmentResponseDTO.class, new MyServiceExecutor<SimpleIdRequestDTO, DepartmentResponseDTO>() {
            @Override
            protected DepartmentResponseDTO myprocess(SimpleIdRequestDTO requestDTO) throws Exception {
                DepartmentResponseDTO responseDTO = new DepartmentResponseDTO();
                DepartmentModel departmentModel = departmentDao.findId(requestDTO.getId());
                DepartmentDTO department = new DepartmentDTO();
                BeanUtils.copyProperties(departmentModel, department);
                responseDTO.setInfo(department);
                responseDTO.successfully();
                return responseDTO;
            }
        });
    }

    /**
     * 列表查询
     *
     * @param requestDTO
     * @return
     */
    public DepartmentListResponseDTO findList(DepartmentRequestDTO requestDTO) {
        return this.service(ServiceActionEnums.DEPARTMENT_FIND_LIST, requestDTO, DepartmentListResponseDTO.class, new MyServiceExecutor<DepartmentRequestDTO, DepartmentListResponseDTO>() {
            @Override
            protected DepartmentListResponseDTO myprocess(DepartmentRequestDTO requestDTO) throws Exception {
                DepartmentListResponseDTO responseDTO = new DepartmentListResponseDTO();
                DepartmentModel req = new DepartmentModel();
                BeanUtils.copyProperties(requestDTO, req);
                List<DepartmentModel> lists = departmentDao.findPage(req);
                List<DepartmentDTO> roles = JSONObject.parseArray(JSONArray.toJSONString(lists), DepartmentDTO.class);
                responseDTO.setInfos(roles);
                responseDTO.successfully();
                return responseDTO;
            }
        });
    }
}
